package com.wehealth;

import java.util.List;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.umeng.analytics.MobclickAgent;
import com.wehealth.Dr_Info;
import com.wehealth.adapter.DrListAdapter;
import com.wehealth.adapter.DrListAdapter.ViewHolder;
import com.wehealth.dm.Doctor;
import com.wehealth.parse.ParseJson;
import com.wehealth.util.DoTask;

public class Dr_List extends Fragment {

	private ListView lv_doctor;
	
	private List<Doctor> dr_list;
	
	MainActivity activity;

	/**
	 * 一个弹框提示
	 */
	ProgressDialog pd = null;
	
	public  Dr_List()
	{
		Log.i("init:", "Dr_List"); 
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		
		return  inflater.inflate(R.layout.dr_list, null);
	}
	
	public void onActivityCreated(Bundle savedInstanceState){
		
		super.onActivityCreated(savedInstanceState);
		
		activity = (MainActivity)getActivity();
		MobclickAgent.openActivityDurationTrack(false);
		
		Button bBack = (Button) activity.findViewById(R.id.bBack_DrList);
		
		lv_doctor = (ListView)activity.findViewById(R.id.lv_dr_list);
		
		lv_doctor.setOnItemClickListener(new OnItemClickListener() {
			@Override
            public void onItemClick(AdapterView<?> parent, View dorctor_view, int pos,
                    long post2) {
				ViewHolder holder = (ViewHolder)dorctor_view.getTag();
				ImageView dorctor_photo = holder.icons;
				Dr_Info drInfo = new Dr_Info(dr_list.get(pos),dorctor_photo); //SIMMON_ADD
				
				if (getActivity() instanceof MainActivity)
				{
					MainActivity activity = (MainActivity) getActivity();
					
					activity.switchContent(drInfo);
				}
			}
		});
		//请求医生列表
		getDrData();
			
		 bBack.setOnClickListener(new View.OnClickListener()
			{
				public void onClick(View v)
				{
					
                    MainActivity activity = (MainActivity) getActivity();                       
                    activity.switchContentHome();
				}
			});
		 
		 

		 /*
		 ImageView bList = (ImageView) activity.findViewById(R.id.temp_drList);
			
		 bList.setOnClickListener(new View.OnClickListener()
			{
				public void onClick(View v)
				{
					
					Dr_Info drInfo = new Dr_Info();
					
					if (getActivity() instanceof MainActivity)
					{
						MainActivity activity = (MainActivity) getActivity();
						
						activity.switchContent(drInfo);
					}
					
				}
			});
			*/
	}
	/**
	 * 渲染界面,显示数据
	 */
	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message paramMessage) {
			//取消加载框
			pd.dismiss();
			//获取请求返回的值
			Bundle bundle = paramMessage.getData();
			String msginfo = bundle.getString("msg");
			if (msginfo.equals("1")) {
				//程序请求失败
				Toast.makeText(activity, "网络连接失败,稍后重试", Toast.LENGTH_LONG).show();
				return;
			}else {
				dr_list = ParseJson.getDrList(msginfo);
				
				lv_doctor.setAdapter(new DrListAdapter(activity, dr_list));
			}
		}
	};
	/**
	 * 获取商家信息
	 */
	public void getDrData() {
		//构建请求地址
		String getShopListUrl = com.wehealth.util.GetUrl.Dr_GetList("-1", "1");
		//可能要换种方式进行读取
		DoTask getDrListTask = new DoTask(activity, handler);
		pd = ProgressDialog.show(activity, "请稍候", "正在获取数据...",false);
		getDrListTask.execute(getShopListUrl);	
	}
	
    @Override
    public void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        MobclickAgent.onPageEnd("dr_List"); 
    }


    @Override
    public void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        MobclickAgent.onPageStart("DR_List"); 
    }
}
